package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/addressBook")
@Api(tags = "地址簿相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 把某个用户的地址都展示出来
     *
     * @return
     */

    @GetMapping("/list")
    public R<List<AddressBook>> showAddress() {
        Long id = BaseContext.getCurrentId();

        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper.lambda().eq(AddressBook::getUserId, id);
        addressBookQueryWrapper.lambda().orderByDesc(AddressBook::getCreateTime);
        List<AddressBook> list = addressBookService.list(addressBookQueryWrapper);
        return R.success(list);
    }

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        if (addressBook != null) {
            Long currentId = BaseContext.getCurrentId();
            addressBook.setUserId(currentId);
            addressBookService.save(addressBook);
            return R.success("地址添加成功");
        } else {
            return R.error("地址添加失败");
        }
    }

    /**
     * 修改前回显操作
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("回显失败");
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        if (addressBook != null) {
            addressBookService.updateById(addressBook);
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 根据id(批量)删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids) {
        if (ids != null) {
            addressBookService.removeByIds(ids);
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 修改默认地址
     *
     * @param map
     * @return
     */

    @PutMapping("/default")
    public R<String> updateToDefaultAddressBook(@RequestBody Map map) {
        Object id = map.get("id");
        log.info("要修改为默认地址的id:{}",id);
        AddressBook addressBook = addressBookService.getById(id.toString());
        //在设置默认地址之前,要把之前的默认地址设置为非默认
        if (addressBook != null) {
            //找出之前地址里面默认的
            QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
            addressBookQueryWrapper.lambda().eq(AddressBook::getIsDefault, 1);
            AddressBook one = addressBookService.getOne(addressBookQueryWrapper);
            if (one!=null){
                one.setIsDefault(0);
                addressBookService.updateById(one);
            }
            //把前端传过来要修改为默认地址的改掉
            addressBook.setIsDefault(1);
            addressBookService.updateById(addressBook);
            return R.success("铁汁,改好了");
        }
        return R.error("铁子是不是有点问题,没改成啊");
    }

    /**
     * 查询默认地址
     * @return
     */
    //查询默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressBook(){
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper.lambda().eq(AddressBook::getIsDefault, 1);
        AddressBook defaultAddress = addressBookService.getOne(addressBookQueryWrapper);
        if (defaultAddress!=null){
            return R.success(defaultAddress);
        }
        return R.error("没查到啊,没设默认地址吧鉄子!");

    }

}
